package com.fifth.apartment.service.impl;

import com.fifth.apartment.service.UVService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @author Dao
 * @since 2024/7/14
 */
@Service
@Slf4j
public class UVServiceImpl implements UVService {

    public static final  String UV = "ip";
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public void visitor(){
        Random random = new Random();

        for (int i = 0; i < 100; i++) {
            String ip = random.nextInt(256)+"."+random.nextInt(256)+"."+random.nextInt(256)+"."+random.nextInt(256);

            new Thread(()->{
                redisTemplate.opsForHyperLogLog().add(UV,ip);
                log.info("{}访问",ip);

            },"T"+(i+1)).start();
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    public Long uvCount() {
        Long size = redisTemplate.opsForHyperLogLog().size(UV);
        visitor();
        return size;
    }
}
